import 'package:flutter/material.dart';
import 'package:flutter2/colors.dart';
import 'package:flutter2/ui/me.dart';
import 'package:flutter2/ui/order.dart';
import 'package:flutter2/utils/string.dart';
import 'package:flutter2/widget/base/base_container.dart';

final ValueNotifier<int> tabNotifier = ValueNotifier(-1);

class MainTabPage extends StatefulWidget {
  const MainTabPage({
    super.key,
    this.onTap,
  });

  final ValueChanged<int>? onTap;

  @override
  State<MainTabPage> createState() => _MainTabPageState();
}

final PageController _controller = PageController(keepPage: true);

class _MainTabPageState extends State<MainTabPage> {
  int _index = 0;

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: PageView(
        physics: NeverScrollableScrollPhysics(),
        children: [
          OrderPage(),
          MePage(),
        ],
        controller: _controller,
      ),
      bottomNavigationBar: BottomNavigationBar(
        fixedColor: btnColor,
        selectedFontSize: 12,
        unselectedFontSize: 12,
        elevation: 0,
        currentIndex: _index,
        onTap: (value) {
          if (value == _index) return;
          _index = value;
          tabNotifier.value = value;
          widget.onTap?.call(value);
          _controller.jumpToPage(value);
          setState(() {});
        },
        items: [
          getItems("ic_tab_order_unchecked", "ic_tab_order_checked", "Pedidos"),
          getItems("ic_tab_user_unchecked", "ic_tab_user_checked", "Perfil"),
        ],
      ),
    );
  }

  BottomNavigationBarItem getItems(String icon, String activeIcon, String label) {
    return BottomNavigationBarItem(
      backgroundColor: Colors.transparent,
      label: label,
      icon: BaseContainer(
        width: 18,
        height: 18,
        backgroundImg: icon.toImage(),
      ),
      activeIcon: BaseContainer(
        width: 18,
        height: 18,
        backgroundImg: activeIcon.toImage(),
      ),
    );
  }
}
